﻿
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.EventSystems;


namespace Modules.Guide.Runtime
{
    public class GuideEvent: MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, 
        IBeginDragHandler, IEndDragHandler, IDragHandler
    {
        //[HideInInspector]
        public GameObject m_target;
        public bool m_clicked = false;
    
        public bool Clicked
        {
            get
            {
                return m_clicked;
            }
            set
            {
                m_clicked = value;
            }
        }
        public void OnPointerDown(PointerEventData eventData)
        {
            m_clicked = false;
        }
        
        public void OnPointerUp(PointerEventData eventData)
        {
            //PassEvent(eventData,ExecuteEvents.pointerUpHandler);
        }
        
        public void OnPointerClick(PointerEventData eventData)
        {
            m_clicked = true;
            Debug.Log($"{transform.name} click!!!");
        }
    
        public void OnBeginDrag(PointerEventData eventData)
        {
            Debug.Log($"{transform.name} OnBeginDrag!!!");
        }
    
        public void OnEndDrag(PointerEventData eventData)
        {
            ;
        }
        
        public void OnDrag(PointerEventData eventData)
        {
            
        }
    
    
        private GameObject PassEvent<T>(PointerEventData data,ExecuteEvents.EventFunction<T> function) where T : IEventSystemHandler
        {
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(data, results); 
            //GameObject current = data.pointerCurrentRaycast.gameObject;
            for(int i = 0; i < results.Count; i++)
            {
                Debug.Log($"--{results[i].gameObject.name}");
                if(m_target == results[i].gameObject)
                {
                    ExecuteEvents.Execute(results[i].gameObject, data, function);
                    return results[i].gameObject;
                }
            }
    
            return null;
        }
    }

}